using UnityEngine;

namespace Exploder.Demo
{
	public class ShotgunController : MonoBehaviour
	{
		public AudioClip GunShot;

		public AudioClip Reload;

		public AudioSource Source;

		public ExploderMouseLook MouseLookCamera;

		public Light Flash;

		public Animation ReloadAnim;

		public AnimationClip HideAnim;

		public GameObject MuzzleFlash;

		private int flashing;

		private float reloadTimeout = float.MaxValue;

		private float nextShotTimeout;

		private TargetType lastTarget;

		public ExploderObject exploder;

		public void OnActivate()
		{
			ExploderUtils.SetActive(MuzzleFlash, status: false);
		}

		private void Update()
		{
			GameObject gameObject = null;
			TargetType targetType = TargetManager.Instance.TargetType;
			if (targetType == TargetType.UseObject)
			{
				if (lastTarget != TargetType.UseObject)
				{
				}
				lastTarget = TargetType.UseObject;
			}
			if (lastTarget == TargetType.UseObject)
			{
			}
			lastTarget = targetType;
			Ray ray = MouseLookCamera.mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f));
			UnityEngine.Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red, 0f);
			if (targetType == TargetType.DestroyableObject)
			{
				gameObject = TargetManager.Instance.TargetObject;
			}
			if (Input.GetMouseButtonDown(0) && nextShotTimeout < 0f && CursorLocking.IsLocked)
			{
				if (targetType != TargetType.UseObject)
				{
					Source.PlayOneShot(GunShot);
					MouseLookCamera.Kick();
					reloadTimeout = 0.3f;
					flashing = 5;
				}
				if ((bool)gameObject)
				{
					Vector3 centroid = ExploderUtils.GetCentroid(gameObject);
					exploder.transform.position = centroid;
					exploder.ExplodeSelf = false;
					exploder.ForceVector = ray.direction.normalized;
					exploder.Force = 10f;
					exploder.UseForceVector = true;
					exploder.TargetFragments = 30;
					exploder.Radius = 1f;
					exploder.ExplodeRadius();
				}
				nextShotTimeout = 0.6f;
			}
			nextShotTimeout -= Time.deltaTime;
			if (flashing > 0)
			{
				Flash.intensity = 1f;
				ExploderUtils.SetActive(MuzzleFlash, status: true);
				flashing--;
			}
			else
			{
				Flash.intensity = 0f;
				ExploderUtils.SetActive(MuzzleFlash, status: false);
			}
			reloadTimeout -= Time.deltaTime;
			if (reloadTimeout < 0f)
			{
				reloadTimeout = float.MaxValue;
				Source.PlayOneShot(Reload);
				ReloadAnim.Play();
			}
		}
	}
}
